package com.niit.onlinemusicserver.controller;

import com.niit.onlinemusicserver.common.R;
import com.niit.onlinemusicserver.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 支付宝沙箱支付接口
 * @Author: Jason
 * @date: 2023.6.29
 */
@Controller
public class PayController {

  @Autowired
  private PayService payService;

  /**
   * 通用支付
   * @param songIds
   * @param userId
   * @param payType
   * @return
   * @throws Exception
   */
  @PostMapping("/pay/common/alipay")
  @ResponseBody
  public R commonAlipay(@RequestParam(value = "songIds") List<Integer> songIds,
                        @RequestParam("userId") Integer userId,
                        @RequestParam("payType") Integer payType) throws Exception {
    return payService.commonAlipay(songIds,userId,payType);
  }

  /**
   * 回调跳转页面
   * @param response
   * @throws Exception
   */
  @GetMapping("/pay/common/return")
  public void commonAlipayReturn(@RequestParam("out_trade_no") String tradeNo,
                                 @RequestParam("sign") String sign,
                                 HttpServletResponse response) throws Exception{
    payService.commonPayReturn(tradeNo, sign, response);
  }

  /**
   * 回调核销订单子项
   * @param request
   * @return
   * @throws Exception
   */
  @PostMapping("/pay/common/notify")
  @ResponseBody
  public R commonAlipayNotify(HttpServletRequest request) throws Exception{
    return payService.commonPayNotify(request);
  }

  /**
   * 返回所有已支付的歌曲清单
   * @return
   */
  @GetMapping("/pay/allPayList")
  @ResponseBody
  public R getPayList(@RequestParam("userId") Integer userId){
    return payService.allPayList(userId);
  }
}